use strict;
use warnings;
package fcn_calc_simil;

sub fcn_calc_simil::openAndLoadFileinMap {
	open (TT, "<$_[0]") || die "fichier non trouvé : $_[0]";
	my %ret = ();
	while (my $ligne = <TT>){
		if($ligne=~ /^(.*)\t(.*)$/){
			$ret{$1}=$2;
		}
	}
	close (TT);
	return \%ret;
}

sub fcn_calc_simil::calcul_similariteDocuments { 
	my %dji = %{&openAndLoadFileinMap($_[0])};
	my %dki = %{&openAndLoadFileinMap($_[1])};
	my $num=0;
	my $den1=0;
	my $den2=0;
	my $tmp;

	for my $k(sort { $a cmp $b } keys(%dji)){
		$tmp=$dji{$k};
		if(exists($dki{$k})){
			$num+=$tmp*$dki{$k};
		}
		$den1+=$tmp**2;
	}

	for my $k(sort { $a cmp $b } keys(%dki)){
		$den2+=$dki{$k}**2;
	}
	#print "$_[0] --> $_[1] : \n\t$num/($den1*$den2)\n";
	return $num/(sqrt($den1)*sqrt($den2));
}

1;
